package vno.sci
{
	import flash.events.MouseEvent;

	/**
	 * 示例用法 
	 * <p>var cnpc:NPCController = new NPCController();</p>
	 * <p>cnpc.addNPC("NPC1","");</p>
	 * <p>this.addChild(cnpc.npc["NPC1"]);</p>
	 */
	public class NPCController
	{
		public var npc:Object = {};
		public var content:Object = {};
		
		public function addNPC(npcID:String, npcImageURL:String):Boolean
		{
			var newNPC:NPC = new NPC(this,npcID,npcImageURL);
			npc[npcID]=newNPC;
			content[npcID] = {};
			content[npcID].index = 0;
			content[npcID].array = new Array("A:","倒数五个数","B:","5","A:","4","B:","3","A:","2","B:","1....消失");
			return true;
		}
		
		//not public functionn
		public function onPrevBtn(npcID:String):Boolean
		{
			if (content[npcID].index > 2)
			{
				content[npcID].index-=2;
				npc[npcID].setTitle(content[npcID].array[content[npcID].index-2]);
				npc[npcID].setText(content[npcID].array[content[npcID].index-1]);
				return true;
			}
			return false;
		}
		
		//not public function
		public function onNextBtn(npcID:String):Boolean
		{
//			trace(content[npcID]);
//			return true;
			if (content[npcID].index < (content[npcID].array as Array).length)
			{
				content[npcID].index+=2;
				npc[npcID].setTitle(content[npcID].array[content[npcID].index-2]);
				npc[npcID].setText(content[npcID].array[content[npcID].index-1]);
				return true;
			}
			return false;
		}
		
		public function monologFinished(id:String):void
		{
			content[id].index = 0;
		}
		
		public function onClick(id:String, event:MouseEvent):void
		{
			(npc[id] as NPC).image.removeEventListener(MouseEvent.CLICK,npc[id].onClick);
			(npc[id] as NPC).showMonolog(event.localX, event.localY);
		}
		public function onFrameClosing(id:String):void
		{
			npc[id].imageAddListener();
		}
		
		
		
		
		public function NPCController()
		{
			
		}
	}
}


import com.mosesSupposes.bitmap.InteractivePNG;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.text.TextField;

import org.aswing.AsWingConstants;
import org.aswing.BorderLayout;
import org.aswing.BoxLayout;
import org.aswing.FlowLayout;
import org.aswing.GridLayout;
import org.aswing.JButton;
import org.aswing.JPanel;
import org.aswing.JScrollPane;
import org.aswing.JTextArea;
import org.aswing.SoftBoxLayout;
import org.aswing.colorchooser.VerticalLayout;
import org.aswing.event.FrameEvent;
import org.osmf.layout.LayoutMode;
import org.osmf.net.StreamingURLResource;

import vno.sci.Monolog;
import vno.sci.NPCController;
import vno.sci.event.MonologEvent;

/**
 * NPC类，继承自InteractivePNG
 * 20130117修改，先用着。
 * @author scinart
 * 
 */	

class NPC extends InteractivePNG
{		
	internal var imageURL:String = "http://127.0.0.1/vno/npc.png";
	
	public var frame:Monolog	
	
	internal var content:Array = new Array("A:","倒数五个数","B:","五","A:","四","B:","三","A:","二","B:","一....消失");
	internal var index:int = 0;/*not interchangeable*/
	internal var modified:Boolean = false;
	
	static internal var frameWidth:uint = 300;
	static internal var frameHeight:uint = 200;
	static internal var textX:int = 0;
	static internal var textY:int= 50;
	
	internal var image:InteractivePNG;
	
	private var npcController:NPCController;
	private var id:String = "";
	
	
	/**
	 * 在测试过程中应该由外部传递到构造函数里的变量在内部事先定义，并声明为internal
	 * 在做测试过程中这些变量可以自由替换
	 */
	public function NPC(npcControllerReference:NPCController, npcID:String, imageURL_:String)
	{
		npcController = npcControllerReference;
		id = npcID;
		if(imageURL_)
		{
			imageURL = imageURL_;
		}
		
		super();
		image = new InteractivePNG();
		var loader:Loader = new Loader();
		loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
		loader.load(new URLRequest(imageURL));
	}
	
	/**
	 * 显示一段独白，没有用户交互
	 * 内容在测试过程中是预定义的好的。
	 * @param x 独白相对对PNG的x坐标
	 * @param y 独白相对对PNG的y坐标
	 * 
	 */		
	public function showMonolog(x:uint = 0, y:uint = 0):void
	{
		//主窗体
		frame = new Monolog(frameWidth,frameHeight,textX,textY);
		//set the position of frame;
		frame.frame.x = x;
		frame.frame.y = y;
		
		var onPrevBtn:Function = function (event:MonologEvent):void
		{
			event.stopImmediatePropagation();
			npcController.onPrevBtn(id);
		}
		var onNextBtn:Function = function (event:MonologEvent):void
		{
			event.stopImmediatePropagation();
			npcController.onNextBtn(id);
			
		}
		var showNextAndClose:Function = function (event:Event):void
		{
			if (npcController.onNextBtn(id))
			{
			}
			else
			{
				frame.removeEventListener(MouseEvent.CLICK,showNextAndClose);
				frame.frame.dispose();
				frame = null;
				monologFinished();
			}
		}
		
		/*frame listeners*/
		frame.addEventListener(MonologEvent.NEXT,onNextBtn);
		frame.addEventListener(MonologEvent.PREVIOUS,onPrevBtn);
		frame.frame.addEventListener(FrameEvent.FRAME_CLOSING,onFrameClosing);
		frame.frame.addEventListener(MouseEvent.CLICK,showNextAndClose);
		/*end*/
		
		
		
		showNextAndClose(new MouseEvent("test"));
		
		this.addChild(frame);
		
	}
	
	/**
	 * 通过代码显式得强制关闭对话。 
	 */
	public function closeMonolog():void
	{
		if(frame)
		{
			frame.frame.closeReleased();
			frame = null;
		}
		imageAddListener();
	}
	
	internal function imageAddListener():void
	{
		image.addEventListener(MouseEvent.CLICK,onClick);
	}
	
	/**
	 * 点击Frame的关闭按钮执行此函数
	 */
	private function onFrameClosing(event:FrameEvent):void
	{
		imageAddListener();
		npcController.onFrameClosing(id);
	}
	
	private function monologFinished():void
	{
		image.addEventListener(MouseEvent.CLICK,onClick);
		npcController.monologFinished(id);
	}
	
	private function onComplete(event:Event):void
	{
		var bitmap:Bitmap = (event.target.content as Bitmap);
		//			var bitmap:BitmapData = image.bitmapData;
		image.addChild(bitmap);
		addChild(image);
		image.addEventListener(MouseEvent.CLICK, onClick);
	}		
	
	internal function onClick(event:MouseEvent):void
	{
		npcController.onClick(id,event);
	}
	
	internal function setTitle(title:String):void
	{
		frame.setTitle(title);
	}
	internal function setText(t:String):void
	{
		frame.setText(t);
	}
}	
